import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import spsh from 'vite-plugin-simple-html'

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  const name = env.VITE_NAME
  const baseUrl = env.VITE_BASEURL
  const apiProxy = env.VITE_API_PROXY
  return defineConfig({
    plugins: [
      react(),
      spsh({
        minify: true,
        inject: {
          data: { name }
        }
      })
    ],
    server: {
      host: '0.0.0.0',
      port: 6868,
      proxy: {
        [baseUrl]: {
          target: apiProxy,
          changeOrigin: true,
          rewrite: (path) => {
            console.log('\x1b[1;38;2;255;255;0mvite:proxy: \x1b[0m' + path + ' -> ' + apiProxy)
            return path
          }
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "./src/assets/scss/global.scss";'
        }
      }
    }
  })
})
